package liangtWorkSpace.threadDemo.thread.JUC;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author liangt
 * @ClassName: CyclocBarrierSimple
 * @title: CyclocBarrierSimple
 * @projectName liangtWorkSpace.threadDemo.thread.JUC
 * @description: 循环屏障 它允许线程互相等待
 * @date 2020/11/5 18:57
 */
public class CyclocBarrierSimple {
    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(5);

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);


        for (int i = 0; i < 100; i++) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    go();
                }
            });
        }
        threadPool.shutdownNow();
    }

    private static void go(){
        System.out.println(Thread.currentThread().getName() + "：准备跑");
        try {
            cyclicBarrier.await();//设置等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"：开始跑");
    }
}
